"""
工作台服务
"""

from typing import List, Dict, Any
from sqlalchemy.orm import Session
from datetime import datetime, timedelta

from app_service import AppService
from message_service import MessageService

class WorkspaceService:
    """工作台服务类"""
    
    def __init__(self, db: Session):
        self.db = db
        self.app_service = AppService(db)
        self.message_service = MessageService(db)
    
    async def get_workspace_apps(self, user_id: int) -> Dict[str, Any]:
        """获取工作台应用"""
        # 获取用户常用应用
        favorite_apps = await self.app_service.get_user_favorites(user_id)
        
        # 获取快速访问入口
        quick_access = [
            {
                "id": "scan",
                "title": "扫码",
                "icon": "qr_code_scanner",
                "route": "/scan",
                "color": "#2196F3",
                "description": "扫描二维码"
            },
            {
                "id": "approval",
                "title": "审批",
                "icon": "approval",
                "route": "/approval",
                "color": "#4CAF50",
                "description": "待办审批"
            },
            {
                "id": "report",
                "title": "报表",
                "icon": "assessment",
                "route": "/report",
                "color": "#FF9800",
                "description": "数据报表"
            },
            {
                "id": "contact",
                "title": "通讯录",
                "icon": "contacts",
                "route": "/contact",
                "color": "#9C27B0",
                "description": "企业通讯录"
            }
        ]
        
        return {
            "favorite_apps": favorite_apps,
            "quick_access": quick_access,
            "total_apps": len(favorite_apps),
            "last_updated": datetime.now().isoformat()
        }
    
    async def get_user_statistics(self, user_id: int) -> Dict[str, Any]:
        """获取用户统计数据"""
        # 获取未读消息数量
        unread_messages = await self.message_service.get_unread_count(user_id)
        
        # 模拟其他统计数据
        statistics = {
            "pending_tasks": 5,
            "completed_tasks": 23,
            "total_messages": 12,
            "unread_messages": unread_messages,
            "this_week_tasks": 8,
            "this_month_tasks": 35,
            "completion_rate": 82.5,
            "last_login": (datetime.now() - timedelta(hours=2)).isoformat()
        }
        
        return statistics
    
    async def get_recent_activities(self, user_id: int, limit: int = 10) -> List[Dict[str, Any]]:
        """获取最近活动"""
        # 模拟最近活动数据
        activities = [
            {
                "id": 1,
                "type": "approval",
                "title": "请假申请已通过",
                "description": "您的年假申请已获得批准",
                "time": (datetime.now() - timedelta(hours=2)).isoformat(),
                "status": "approved",
                "icon": "check_circle",
                "color": "#4CAF50"
            },
            {
                "id": 2,
                "type": "message",
                "title": "收到新消息",
                "description": "来自张经理的工作安排",
                "time": (datetime.now() - timedelta(hours=5)).isoformat(),
                "status": "unread",
                "icon": "message",
                "color": "#2196F3"
            },
            {
                "id": 3,
                "type": "task",
                "title": "任务已完成",
                "description": "月度报告整理任务已完成",
                "time": (datetime.now() - timedelta(days=1)).isoformat(),
                "status": "completed",
                "icon": "task_alt",
                "color": "#FF9800"
            },
            {
                "id": 4,
                "type": "system",
                "title": "系统更新",
                "description": "移动端应用已更新至最新版本",
                "time": (datetime.now() - timedelta(days=2)).isoformat(),
                "status": "info",
                "icon": "system_update",
                "color": "#9C27B0"
            },
            {
                "id": 5,
                "type": "approval",
                "title": "报销申请待审批",
                "description": "差旅费报销申请等待审批",
                "time": (datetime.now() - timedelta(days=3)).isoformat(),
                "status": "pending",
                "icon": "pending",
                "color": "#FFC107"
            }
        ]
        
        return activities[:limit]